//
//  ByShopProductDetailVC.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/11/1.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByShopProductDetailVC.h"

// view
#import "ByViewProductDetailToolBar.h"                  // 工具栏
#import "ByViewProductDetailCommentHeader.h"            // 评论头视图
#import "ByViewSkuSelect.h"                             // 规格选择

// cell
#import "ByCellProductDetailVideoHeader.h"              // 视频
#import "ByCellProductDetailProductionDescription.h"    // 商品描述
#import "ByCellProductDetailServices.h"                 // 服务
#import "ByCellProductDetailStoreInfo.h"                // 商家信息
#import "ByCellProductDetailComment.h"                  // 评价
#import "ByCellProductDetailInfo.h"                     // 商品详情
#import "ByCellShopProduct.h"                           // 商品

@interface ByShopProductDetailVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) ByViewProductDetailToolBar *toolBar;

@property (nonatomic, strong) ByModelProductDetail *detailModel;

@end

@implementation ByShopProductDetailVC
{
    
}

#pragma mark - view method
- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"商品详情";
    
    [self addLeftBackBarButtonItem];
    
    [self createViews];
    
    [self getDetail];
}

#pragma mark - 请求数据
- (void)getDetail {
    
    // 模拟请求
    _detailModel = [ByModelProductDetail new];
    
    _detailModel.desc = @"耐克最新球鞋耐克最新球鞋耐克最新球鞋耐克最新球鞋耐克最新球鞋耐克最新球鞋耐克最新球鞋耐克最新球鞋耐克最新球鞋耐克最新球鞋";
    _detailModel.price = 8888.88;
    _detailModel.originPrice = 9999.99;
    _detailModel.tags = @[@"积分抵扣", @"自营"];
    _detailModel.services = @[@"同城自取", @"同城配送", @"7天退换", @"假一罚十"];
    _detailModel.commentCount = 100;
    
    ByModelProductComment *comment1 = [ByModelProductComment new];
    comment1.nickname = @"张三";
    comment1.content = @"MPMoviePlayerController足够强大，几乎不用写几行代码就能完成一个播放器，但是正是由于它的高度封装使得要自定义这个播放器变得很复杂，甚至是不可能完成。例如有些时候需要自定义播放器的样式，那么如果要使用MPMoviePlayerController就不合适了，如果要对视频有自由的控制则可以使用";
    comment1.hasVideo = true;
    comment1.skuDesc = @"颜色：藏蓝；尺寸：43";
    comment1.date = @"2017-09-13";
    comment1.rank = ByProductCommentRankGood;
    
    ByModelProductComment *comment2 = [ByModelProductComment new];
    comment2.nickname = @"李四";
    comment2.content = @"一般";
    comment2.hasVideo = false;
    comment2.skuDesc = @"颜色：藏蓝；尺寸：43";
    comment2.date = @"2017-09-13";
    comment2.rank = ByProductCommentRankMid;
    
    ByModelProductComment *comment3 = [ByModelProductComment new];
    comment3.nickname = @"赵武";
    comment3.content = @"差评，不解释";
    comment3.hasVideo = false;
    comment3.skuDesc = @"颜色：藏蓝；尺寸：43";
    comment3.date = @"2017-09-13";
    comment3.rank = ByProductCommentRankBad;
    
    _detailModel.comments = @[comment1, comment2, comment3];
    
    
    SkuNameModel *nameModel1 = [[SkuNameModel alloc] init];
    nameModel1.sid_des = @"尺寸";
    nameModel1.vidAry_des = @[@"大", @"很小"];
    SkuNameModel *nameModel2 = [[SkuNameModel alloc] init];
    nameModel2.sid_des = @"颜色";
    nameModel2.vidAry_des = @[@"非常黑", @"白"];
    
    SkuIdModel *idModel1 = [[SkuIdModel alloc] init];
    idModel1.sid = @"10";
    idModel1.vidAry = @[@"11", @"12"];
    SkuIdModel *idModel2 = [[SkuIdModel alloc] init];
    idModel2.sid = @"20";
    idModel2.vidAry = @[@"21", @"22"];
    
    SkuModel *model1 = [[SkuModel alloc] init];
    model1.sku_desc = @"大;非常黑";
    model1.sku_id = @"10:11;20:21";
    model1.quantity = @"12";
    model1.price = @"1111";
    SkuModel *model2 = [[SkuModel alloc] init];
    model2.sku_desc = @"很小;非常黑";
    model2.sku_id = @"10:12;20:21";
    model2.quantity = @"11";
    model2.price = @"2222";
    SkuModel *model3 = [[SkuModel alloc] init];
    model3.sku_desc = @"大;白";
    model3.sku_id = @"10:11;20:22";
    model3.quantity = @"12";
    model3.price = @"3333";
    
    _detailModel.skuAry = @[model1, model2, model3];
    _detailModel.skuIdAry = @[idModel1, idModel2];
    _detailModel.skuNameAry = @[nameModel1, nameModel2];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [_table reloadData];
    });
}

#pragma mark - 创建视图
- (void)createViews {
    
    __weak typeof(self) wself = self;
    
    // 工具栏
    _toolBar = [[ByViewProductDetailToolBar alloc] initWithFrame:CGRectZero mainTitles:@[@"加入购物车", @"立即购买"] mainFgColors:@[[UIColor whiteColor], BY_GOLD_COLOR] mainBgColors:@[BY_GOLD_COLOR, BY_THEME_COLOR] mainRatio:0.5 subTitles:@[@"店铺", @"收藏", @"客服"] subImages:@[@"", @"", @""]];
    [self.view addSubview:_toolBar];
    _toolBar.functionClickBlock = ^(NSInteger index, BOOL selected) {
        if (index == 3 || index == 4) {
            ByViewSkuSelect *skuView = [ByViewSkuSelect new];
            skuView.detailModel = wself.detailModel;
            skuView.okBlock = ^{
                
            };
            [skuView show];
        }
    };
    [_toolBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.height.equalTo(@49);
        if (@available(ios 11.0,*)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.equalTo(@0);
        }
    }];
    
    // 标示图
    self.table = [[UITableView alloc] init];
    self.table.backgroundColor = BY_BG_COLOR;
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_toolBar);
        make.top.equalTo(@0);
        make.bottom.equalTo(_toolBar.mas_top);
    }];
    
    // 注册
    [self.table registerClass:[ByCellProductDetailVideoHeader class] forCellReuseIdentifier:@"videoCell"];
    [self.table registerClass:[ByCellProductDetailProductionDescription class] forCellReuseIdentifier:@"productDescCell"];
    [self.table registerClass:[ByCellProductDetailServices class] forCellReuseIdentifier:@"servicesCell"];
    [self.table registerClass:[ByCellProductDetailStoreInfo class] forCellReuseIdentifier:@"storeCell"];
    [self.table registerClass:[ByCellProductDetailComment class] forCellReuseIdentifier:@"commentCell"];
    [self.table registerClass:[ByCellProductDetailInfo class] forCellReuseIdentifier:@"detailInfoCell"];
    [self.table registerClass:[ByCellShopProduct class] forCellReuseIdentifier:@"productCell"];
    
    
    [self.table registerClass:[ByViewProductDetailCommentHeader class] forHeaderFooterViewReuseIdentifier:@"commentHeader"];

}
- (void)createTableView {
    
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    /*
     0：视频视图
     1：商品描述
     2：商品标签（同城自取，7天包换等）
     3：商家描述
     4：商品评价（最多显示3条，有头视图，点击头视图看全部）
     5：商品详情（视频+web or image）
     6：相关推荐（其他商品）
     */
    return 7;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 4) {
        return _detailModel.comments.count;
    }
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // 0：视频视图
    if (indexPath.section == 0) {
        ByCellProductDetailVideoHeader *cell = [tableView dequeueReusableCellWithIdentifier:@"videoCell"];
        return cell;
    }
    // 1：商品描述
    else if (indexPath.section == 1) {
        ByCellProductDetailProductionDescription *cell = [tableView dequeueReusableCellWithIdentifier:@"productDescCell"];
        cell.model = _detailModel;
        return cell;
    }
    // 2：商品服务（同城自取，7天包换等）
    else if (indexPath.section == 2) {
        ByCellProductDetailServices *cell = [tableView dequeueReusableCellWithIdentifier:@"servicesCell"];
        cell.model = _detailModel;
        return cell;
    }
    // 3：商家描述
    else if (indexPath.section == 3) {
        ByCellProductDetailStoreInfo *cell = [tableView dequeueReusableCellWithIdentifier:@"storeCell"];
        return cell;
    }
    // 4：商品评价（最多显示3条，有头视图，点击头视图看全部）
    else if (indexPath.section == 4) {
        ByCellProductDetailComment *cell = [tableView dequeueReusableCellWithIdentifier:@"commentCell"];
        cell.model = _detailModel.comments[indexPath.row];
        return cell;
    }
    // 5：商品详情（视频+web or image）
    else if (indexPath.section == 5) {
        ByCellProductDetailInfo *cell = [tableView dequeueReusableCellWithIdentifier:@"detailInfoCell"];
        return cell;
    }
    // 6：相关推荐（其他商品）
    else if (indexPath.section == 6) {
        ByCellShopProduct *cell = [tableView dequeueReusableCellWithIdentifier:@"productCell"];
        return cell;
    }
    
    return [UITableViewCell new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 0：视频视图
    if (indexPath.section == 0) {
        return SCREEN_WIDTH / 2;
    }
    // 1：商品描述
    else if (indexPath.section == 1) {

        return [tableView fd_heightForCellWithIdentifier:@"productDescCell" cacheByIndexPath:indexPath configuration:^(ByCellProductDetailProductionDescription *cell) {
            cell.model = _detailModel;
        }];
    }
    // 2：商品标签（同城自取，7天包换等）
    else if (indexPath.section == 2) {
        return 44;
    }
    // 3：商家描述
    else if (indexPath.section == 3) {
        return 100;
    }
    // 4：商品评价（最多显示3条，有头视图，点击头视图看全部）
    else if (indexPath.section == 4) {
        return [tableView fd_heightForCellWithIdentifier:@"commentCell" cacheByIndexPath:indexPath configuration:^(ByCellProductDetailComment *cell) {
            cell.model = _detailModel.comments[indexPath.row];
        }];
    }
    // 5：商品详情（视频+web or image）
    else if (indexPath.section == 5) {
        return [tableView fd_heightForCellWithIdentifier:@"detailInfoCell" cacheByIndexPath:indexPath configuration:^(ByCellProductDetailInfo *cell) {

        }];
    }
    // 6：相关推荐（其他商品）
    else if (indexPath.section == 6) {
        return (SCREEN_WIDTH - 20) / 2 + 40 + 10;
    }

    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 4 || section == 5 || section == 6) {
        return 40;
    }
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (section == 4) {
        
        ByViewProductDetailCommentHeader *header = (ByViewProductDetailCommentHeader *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"commentHeader"];
        header.model = _detailModel;
        return header;
    }
    else if (section == 5 || section == 6) {
        UIView *header = [UIView new];
        
        // 线条
        UIView *line = [UIView new];
        line.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
        [header addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(header).multipliedBy(0.5);
            make.centerX.equalTo(@0);
            make.centerY.equalTo(@0);
            make.height.equalTo(@1);
        }];
        
        UILabel *label = [[UILabel alloc] init];
        label.backgroundColor = tableView.backgroundColor;
        label.font = FONT(14);
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor lightGrayColor];
        [header addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@80);
            make.top.bottom.equalTo(@0);
            make.centerX.equalTo(@0);
        }];
        
        if (section == 5) {
            label.text = @"商品详情";
        } else {
            label.text = @"相关推荐";
        }
        
        return header;
    }
    return UIView.new;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 3) {
        return 10;
    }
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return UIView.new;
}




@end
